package com.derbysoft.lowcostadapter.dswitch3.translate.impl;

import com.derbysoft.dswitch.dto.hotel.avail.FreeMealDTO;
import com.derbysoft.lowcostadapter.commons.dto.LoginDetails;
import com.derbysoft.lowcostadapter.commons.exception.HotelNotFoundException;
import com.derbysoft.lowcostadapter.commons.exception.IdentificationNotFoundException;
import com.derbysoft.lowcostadapter.commons.model.Hotel;
import com.derbysoft.lowcostadapter.commons.model.ProviderIdentification;
import com.derbysoft.lowcostadapter.commons.model.Status;
import com.derbysoft.lowcostadapter.commons.redis.service.HotelCacheService;
import com.derbysoft.lowcostadapter.commons.redis.service.ProviderIdentificationCacheService;
import com.derbysoft.lowcostadapter.commons.repository.HotelRepository;
import com.derbysoft.lowcostadapter.commons.repository.ProviderIdentificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * Created by: jason
 * Date: 2012-06-18
 */
public abstract class AbstractDSwitch3Translator {

    public static final String DEFAULT_CURRENCY = "GBP";
    protected static final int DEFAULT_CHILD_AGE = 10;
    public static final String BLANK = " ";

    @Autowired
    protected HotelRepository hotelRepository;

    @Autowired
    protected ProviderIdentificationCacheService providerIdentificationCacheService;

    @Autowired
    protected ProviderIdentificationRepository providerIdentificationRepository;

    @Autowired
    protected HotelCacheService hotelCacheService;

    @Value("${provider}")
    protected String provider;

    @Value("${dswitch3.adapter}")
    protected String adapter;

    protected LoginDetails createLoginDetails(String channelCode) {
        LoginDetails loginDetails = new LoginDetails();
        ProviderIdentification identification = loadByChannel(channelCode);
        loginDetails.setLogin(identification.getUserName());
        loginDetails.setPassword(identification.getPassWd());
        loginDetails.setAgentReference(identification.getAgentReference());
        return loginDetails;
    }

    protected ProviderIdentification loadByChannel(String channelCode) {
        ProviderIdentification identification = providerIdentificationCacheService.get(channelCode);
        if (identification == null) {
            identification = providerIdentificationRepository.loadByChannel(channelCode);
        }
        if (identification == null) {
            throw new IdentificationNotFoundException(channelCode);
        }
        return identification;
    }

    protected Hotel loadHotel(String propertyId) {
        Hotel hotel = hotelCacheService.loadById(propertyId);
        if (hotel == null) {
            hotel = hotelRepository.findByPropertyID(Integer.parseInt(propertyId), Status.ENABLED);
            if (hotel == null) {
                throw new HotelNotFoundException(propertyId);
            }
        }
        return hotel;
    }

    protected static FreeMealDTO createFreeMeal(int mealBasisID) {
        FreeMealDTO freeMealDTO = new FreeMealDTO();
        DSwitch3MealBasis dSwitch3MealBasis = DSwitch3MealBasis.fromID(mealBasisID);
        freeMealDTO.setType(dSwitch3MealBasis.getFreeMealType());
        freeMealDTO.setQuantity(1);
        freeMealDTO.setDescription(dSwitch3MealBasis.getDescription());
        return freeMealDTO;
    }

    public void setHotelRepository(HotelRepository hotelRepository) {
        this.hotelRepository = hotelRepository;
    }

    public void setHotelCacheService(HotelCacheService hotelCacheService) {
        this.hotelCacheService = hotelCacheService;
    }

    public void setProvider(String provider) {
        this.provider = provider;
    }

    public void setAdapter(String adapter) {
        this.adapter = adapter;
    }

    public void setProviderIdentificationCacheService(ProviderIdentificationCacheService providerIdentificationCacheService) {
        this.providerIdentificationCacheService = providerIdentificationCacheService;
    }

    public void setProviderIdentificationRepository(ProviderIdentificationRepository providerIdentificationRepository) {
        this.providerIdentificationRepository = providerIdentificationRepository;
    }
}
